<?php

namespace app\home\controller;

use app\home\controller\Common;
use function PHPSTORM_META\elementType;
use think\Db;
use think\facade\Env;

class Speed extends Common
{
    public $arr = array();
    public function index()
    {
        return view();
    }
    //旧版图片生成start
    public function mergeImg($dir,$px,$type){
        $images =array();
        $arr  =array();
// Open a directory, and read its contents
        if (is_dir($dir)){
            if ($dh = opendir($dir)){
                while (($file = readdir($dh)) !== false){
                    if (strlen($file)>3){
                        $images[]= $dir.$file;
                        $str = substr($file,-26,-4);
                        $end = substr($str,-3);
                        $str = substr($str,1,14);
                        $str = strtotime($str);
                        $arr[] = $str.$end;

                    }
                }
                closedir($dh);
            }
        }
        $this->arr = $arr;
//        $target = './template/Speed/static/test.png'; //背景图片
        $target_img = imagecreatetruecolor(800,count($images));
        $text_color = imagecolorallocate($target_img, 	60,60,60);
        imagestring($target_img, 1, 5, 5,  " ", $text_color);
        $source = array();
        foreach ($images as $k => $v) {
            $source[$k]['source'] = Imagecreatefrompng($v);
            $source[$k]['size'] = getimagesize($v);
        }
        $dst_x = 0;//整图左边距距离
        $dst_y = 0;//整图上边距距离
        $src_x = 0;//合成两图X轴间距
        $src_y = 0; //合成两图Y轴间距
        for ($i = 0; $i < count($images); $i++) {
            imagecopy($target_img, $source[$i]['source'], $dst_x, $dst_y, $src_x, $src_y, $source[$i]['size'][0], $source[$i]['size'][1]);
            if ($type == 'y') {
                $dst_y = $dst_y + $source[$i]['size'][1];
                $dst_y -= $px ;
            }
            if ($type == 'x') {
                $dst_x = $dst_x + $source[$i]['size'][0];
                $dst_x -= $px ;
            }


        }
        imagepng($target_img, './public/Speed/pin.png');

    }
    public function img()
    {
        $this->mergeImg('./public/speed/static/images/1/44/','3','y');
        $arr  = array_reverse($this->arr);
        $source = imagecreatefrompng('./public/speed/pin.png');
        $rotate = imagerotate($source, 270, 0);
        imagepng($rotate,'./public/speed/pin.png',0);
        $start_time = '1636358921201';
        $this->assign('arr', $arr);
        $this->assign('start_time', $start_time);

        return view();
    }
    //图片循环加载
    public function getImg()
    {
        $this->mergeImg('./public/speed/static/images/1/44/','3','y');
        $arr  = array_reverse($this->arr);
        $source = imagecreatefrompng('./public/speed/pin.png');
        $rotate = imagerotate($source, 270, 0);
        imagepng($rotate,'./public/speed/pin.png',0);
        $json['status'] = 0;
        $json['arr'] = $arr;
        $json['t'] = time();
        echo json_encode($json);
        exit;
    }
    //旧版图片生成end
    //新版图片生成start
    public function getTxt($file){
        header("content-type:text/html;charset=utf-8");
###判断该文件是否存在
        if(file_exists($file)){
            $file_arr = file($file); ###得到数组
            $arr_new = array();
####对数组的处理
            foreach($file_arr as $v){
                $a = trim($v);
                $a = str_replace("\r\n","",$a);
                $a = str_replace("\r","",$a);
                $a = str_replace("\n","",$a);
                $arr_new[] = $a;
            }
            return $arr_new;
#####接下来就是对数组数据的取出#####
        }else{
            echo "file not exists!";
        }
    }
    public function mergeImg_new($dir){
        $arr  =array();
        $images=array();
// Open a directory, and read its contents
        if (is_dir($dir)){
            if ($dh = opendir($dir)){
                while (($file = readdir($dh)) !== false){
                    if (strlen($file)>13){
                        $arr[]= $dir.$file;
                        $images[]=$file;
                    }
                }
                closedir($dh);
            }
        }
        return $images;
    }
    public function img_new()
    {
        $arr  =  $this->mergeImg_new('./public/speed/result/');
        $start_time = '1636358921201';
        $this->assign('arr', $arr);
        $this->assign('start_time', $start_time);

        return view();
    }
    //图片循环加载
    public function getImg_new()
    {
        $arr = $this->mergeImg_new('./public/speed/result/');
        $txt = $this->getTxt('./public/speed/result/record.txt');
        $json['status'] = 0;
        if($_GET['type']==1){
            //排名从左到右
            $json['arr'] = $arr;
            $json['txt'] = $txt;
        }else{
            //排名从右到左
            $json['arr'] = array_reverse($arr);
            $json['txt'] = array_reverse($txt);
        }
        $json['t'] = time();
        echo json_encode($json);
        exit;
    }
	
	public function resetResult(){
		$basrPath = "./public/speed/result/";
		$dirs = scandir($basrPath);
		if( !empty($dirs) ){
			foreach($dirs as $dir){
				if( $dir != '.' && $dir != '..' ){
					@unlink($basrPath.$dir);
				}
			}
		}
	}
	
}
